package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.ChargeProjectMapper;
import com.kkb.mapper.ExtralChargeProjectMapper;
import com.kkb.pojo.ChargeProject;
import com.kkb.pojo.ChargeProjectExample;
import com.kkb.pojo.DoctorExample;
import com.kkb.vo.ChargeProjectQueryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ChargeProjectService {

    @Resource
    ChargeProjectMapper chargeProjectMapper;
    @Resource
    ExtralChargeProjectMapper extralChargeProjectMapper;

    public List queryAll(ChargeProjectQueryVO vo) {
        ChargeProjectExample chargeProjectExample = new ChargeProjectExample();
        ChargeProjectExample.Criteria criteria = chargeProjectExample.createCriteria();
        if (vo.getName() != null && !vo.getName().trim().equals("")) {
            criteria.andNameLike("%" + vo.getName().trim() + "%");
        }
        if (vo.getId() != null && !vo.getId().trim().equals("")) {
            criteria.andIdEqualTo(Integer.parseInt(vo.getId()));
        }

        return chargeProjectMapper.selectByExample(chargeProjectExample);
    }

    public ChargeProject queryById(int id) {
        return chargeProjectMapper.selectByPrimaryKey(id);
    }

    public PageInfo<ChargeProject> selectInPage(Integer index, Integer size, String name){
        PageHelper.startPage(index,size);
        List<ChargeProject> chargeProjects = extralChargeProjectMapper.selectInPage(name);
        return new PageInfo<ChargeProject>(chargeProjects);
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int deleteById(Integer id){
        return chargeProjectMapper.deleteByPrimaryKey(id);
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int updateChargeProject(ChargeProject project){
        return chargeProjectMapper.updateByPrimaryKeySelective(project);
    }
}
